/*
 * Created by Storm Zhang, Feb 11, 2014.
 */

package com.isunland.managesystem.common;

import android.app.ActivityManager;
import android.content.Context;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class RequestManager {
  private static final String TAG = "RequestManager";
  private static RequestQueue mRequestQueue;
  private static ImageLoader mImageLoader;
  private static BitmapLruCache mBitmapLruCahce;
  private static DefaultRetryPolicy mRetryPolicy;
  public static CookieManager sCookieManager;

  private RequestManager() {
    // no instances
  }

  public static void ClearCookie() {
    sCookieManager.getCookieStore().removeAll();
  }

  public static BitmapLruCache getBitmapLruCache() {
    if (mBitmapLruCahce != null) {
      return mBitmapLruCahce;
    } else {
      throw new IllegalStateException("bitmapLruCache not initialized");
    }
  }

  public static DefaultRetryPolicy getRetryPolicy() {
    if (mRetryPolicy == null) {
      mRetryPolicy = new DefaultRetryPolicy((int) TimeUnit.SECONDS.toMillis(10), 0,
          DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }
    return mRetryPolicy;
  }

  public static void init(Context context) {
    sCookieManager = new CookieManager();
    CookieHandler.setDefault(sCookieManager);
    mRequestQueue = Volley.newRequestQueue(context);

    int memClass =
        ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    // Use 1/8th of the available memory for this memory cache.
    int cacheSize = 1024 * 1024 * memClass / 8;
    mBitmapLruCahce = new BitmapLruCache(cacheSize);

    mImageLoader = new ImageLoader(mRequestQueue, mBitmapLruCahce);
    mRetryPolicy = new DefaultRetryPolicy((int) TimeUnit.SECONDS.toMillis(15), 0,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
  }

  public static RequestQueue getRequestQueue(Context context) {
    if (mRequestQueue == null) {

      mRequestQueue = Volley.newRequestQueue(context.getApplicationContext());
    }
    return mRequestQueue;
  }

  public static void addRequest(Request<?> request, Object tag) {
    if (tag != null) {
      request.setTag(tag);
    }
    mRequestQueue.add(request);
  }

  public static void cancelAll(Object tag) {
    mRequestQueue.cancelAll(tag);
  }

  /**
   * Returns instance of ImageLoader initialized with {@see FakeImageCache}
   * which effectively means that no memory caching is used. This is useful
   * for images that you know that will be show only once.
   */
  public static ImageLoader getImageLoader() {
    if (mImageLoader != null) {
      return mImageLoader;
    } else {
      throw new IllegalStateException("ImageLoader not initialized");
    }
  }

  /**
   * 返回当前cookieStore中的Cookie数据.
   */
  public static String getCookie() {
    List<HttpCookie> cookies = RequestManager.sCookieManager.getCookieStore().getCookies();

    String cookie = "";
    for (HttpCookie httpCookie : cookies) {
      //只有JSESSIONID拼接上
      if (httpCookie.getName().equalsIgnoreCase("JSESSIONID")) {
        String jsessionId = httpCookie.getValue();
        cookie = "JSESSIONID=" + jsessionId;
      }
      //同时又有serverid.再拼接上.
      if (httpCookie.getName().equalsIgnoreCase("SERVERID")) {
        String serverId = httpCookie.getValue();
        cookie = ";SERVERID=" + serverId;
      }
    }

    return cookie;
  }
}
